package servlet;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import to.OrdenPedidoTO;

public class EliminarCarritoPedido extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        procesar(req, resp);
    }
    
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
        procesar(req, resp);
    }
    
    private void procesar(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        
        int item = Integer.parseInt(req.getParameter("item"));
        
        try{
            OrdenPedidoTO to = (OrdenPedidoTO) req.getSession().getAttribute("carritoPedidos");
            to.getList().remove(item);
            req.getSession().setAttribute("carritoPedidos", to);
            getServletContext().getRequestDispatcher("/Registro_Pedido.jsp").forward(req, resp);
        }
        catch (Exception e) {
            PrintWriter out = resp.getWriter();
            out.println("<HTML>");
            out.println("<HEAD>");
            out.println("</HEAD>");
            out.println("<BODY>");

            String msg = (String) e.getMessage();
            out.println("<P>" + msg + "</P>");

            out.println("</BODY>");
            out.println("</HTML>");
	}
    }
}